// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database();
const _ = db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  var dbName = event.dbName;
  var filter = event.filter ? event.filter : null;
  var pageIndex = event.pageIndex ? event.pageIndex : 1;
  var pageSize = event.pageSize ? event.pageSize : 10;
  const countResult = await db.collection(dbName).where(filter).count();
  const total = countResult.total;
  const totalPage = Math.ceil(total / 10);
  var hasMore;
  if (pageIndex > totalPage || pageIndex == totalPage) {
    hasMore = false;
  } else {
    hasMore = true;
  }

  //如查存在command查询,1、单词测评使用
  if (null != filter)
  {
    if (filter.command != null && filter.key != null && filter.val != null)
    {
      let val = filter.val;
      filter = {
        level: _.gte(val)
      };
    }
  }
  
  
  return db.collection(dbName)
    .where(filter)
    .skip((pageIndex - 1) * pageSize)
    .limit(pageSize).get()
    .then(res => {
      res.hasMore = hasMore;
      return res;
    });
}